<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="css/excite-bike/jquery-ui-1.8.22.custom.css"
	rel="stylesheet" type="text/css" />
<link href="css/table.css" rel="stylesheet" type="text/css" />
<script src="js/jquery-1.7.2.min.js"></script>
<script src="js/jquery.cookie.js"></script>
<script src="js/jquery-ui-1.8.22.custom.min.js"></script>
<script src="js/widgets.js"></script>

<script type="text/javascript">
	function getValue(val) {
		var url = window.location.toString(); //取得當前網址
		if (url.indexOf("?") != -1) {
			var ary = url.split("?")[1].split("&");
			//取得"?"右邊網址後利用"&"分割字串存入ary陣列 ["a=1","b=2","c=3"]
			for ( var i in ary) {
				//取得陣列長度去跑迴圈，如:網址有三個參數，則會跑三次
				str = ary[i].split("=")[0];
				//取得參數"="左邊的值存入str變數中
				if (str == val) {
					str_value = decodeURI(ary[i].split("=")[1]);
				}
			}
		}
		return str_value;
	}
	function radio(tcID, uID, isActivated) {
		var checked = (isActivated == 1) ? "CHECKED" : "";
		var tt = "<input value='" + uID
				+ "' type='radio' id='r_" + uID + "' class='activeRadio' onclick='radioClick(\""+tcID+"\",\""
				+ uID + "\")' name='" + tcID + "' " + checked + "  />";
		return tt;
	}


	function radioClick(tcID,Des_uID) {
		$('button').attr("disabled", false);
		$('button[uid=' + Des_uID + ']').attr("disabled", true);

		var n = $('#presentUID')[0].value;
		$('#dialog-message').html('確定更動案例' + tcID + '的對應關係？');

		$("#dialog:ui-dialog").dialog("destroy");
		$("#dialog-message")
				.dialog(
						{
							resizable : false,
							height : 200,
							modal : true,
							buttons : {
								"確定" : function() {
									$('#presentUID')[0].value = $('.activeRadio:checked')[0].value;
									$('#square').html('');	//效果不太一樣，頂著先
									x = $('#q_form_mapping').serializeArray();
									$.ajax({
												type : 'post',
												url : 'UpdateMapping',
												data : x,
												dataType : 'text',
												success : function(updateRes) {
													$('#square').html(updateRes);
												},error : function() {
													$('#q_message').html('使用AJax失敗');
												}
											});
									
									
									
									$(this).dialog("close");
								},
								"返回" : function() {
									var m = $('#presentUID')[0].value;				//取得原本radio點的 uID
									$('#r_'+m).attr('checked','checked');			//將原本的uID，設為  checked 
									$('button').attr("disabled", false);			//將所有的按鈕都設成  可按
									$('button[uid='+m+']').attr("disabled", true);	//將所選radio上的Button設成不可按
									$(this).dialog("close");
								}
							}
						});
	}

	/* 刪除鈕 */
	function delButC(uID) {	
		$('#dialog-message').html('真的要刪除？');
		$("#dialog:ui-dialog").dialog("destroy");
		$('#dialog-message').dialog({
			resizable : false,
			height : 200,
			modal : true,
			buttons : {
				"確定" : function() {
					$('#clickUID')[0].value = uID;	//將要刪除的uID寫入form中的INPUT中準備送出，而tcID在畫面初始時已經設好了
					x = $('#q_form_mapping').serializeArray();
					
					$.ajax({
						type : 'post',
						url : 'DeleteTC',
						data : x,
						dataType : 'TEXT',
						success : function(deleteRes) {
							alert(deleteRes);
						},error : function() {
							alert('使用AJax失敗');
						}
					});
					
					$('#tr_'+uID).remove();
					refreshUI("updateTCTable_b",1);
					$(this).dialog("close");
				},
				"返回" : function() {
					$(this).dialog("close");
				}
			}
		});
	}
	
	/* 產生刪除鈕的Code */
	function deleteBut(tcID, uID, isActivated) {
		var disabled = '';

		if (isActivated == 1) {
			disabled = 'disabled';
			$('#presentUID')[0].value = uID;
		}

		var tt = "<button uid='" + uID + "' class='deleteBut' name='delBut_"
				+ tcID + "' id='delBut_" + tcID + "' onclick='delButC(\"" + uID
				+ "\")' " + disabled + " >刪除</button>";

		return tt;
	}

	$(document).ready(
			function() {
				$('button').button();
				//$('#dialog-message').css("visibility", "hidden");
				$("#loginUser")[0].value = $.cookie("userID");
				
				
				$('#but_tc_back').bind('click', function() {
					location.href = 'Pluto.html';
				});

				$('#q_tcID')[0].value = getValue('tcID');

				x = $('#q_form_mapping').serializeArray();
				$.ajax({
					type : 'post',
					url : 'MappingEdit',
					data : x,
					dataType : 'JSON',
					success : function(queryResult) {
						if (queryResult.mappingData.value == 'No Result') {
							alert('沒找到!!');
						} else {
							//$('#square').html(queryResult.strSQL[0]['strSQL']); //為了看執行SQL語句用
							$.each(queryResult.mappingData, function(i, res) {
								var bgcolor = "#CCCCCC";
								if ((i + 1) % 2 == 1) {
									bgcolor = "#FFFFFF";
								}
								var u = res.uID;
								var tcId = res.tcID;
								$("#th_tcID").html(tcId);
								$('#updateTCTable_b').append(
										'<tr bgcolor=\''+bgcolor+'\' id=tr_'+u+' ><td>'
												+ (i + 1)
												+ '</td><td>'
												+ u
												+ '</td><td>'
												+ radio($('#q_tcID')[0].value,
														u,
														res.isActivated)
												+ '</td><td>'
												+ res.tcName
												+ '</td><td>'
												+ res.numKeyPoint
												+ '</td><td>'
												+ res.tcKeyPoint
												+ '</td><td>'
												+ res.tcTestData
												+ '</td><td>'
												+ res.tcProcess
												+ '</td><td>'
												+ res.tcResExp
												+ '</td><td>'
												+ res.CreatedDateTime
												+ '</td><td>'
												+ res.Creator
												+ '</td><td>'
												+ deleteBut(
														$('#q_tcID')[0].value,
														u,
														res.isActivated)
												+ '</td></tr>');
							});
						}
					},
					error : function() {
						$('#square').html('使用AJax失敗');
					}
				});

			});
</script>
<title>編輯測試案例對應</title>
</head>
<body>
	***************************
	<br /> ~ 編輯測試案例對應 ~
	<br /> ***************************
	<br />
	<p>
	<p />
	<table id='updateTCTable'>
		<thead id='updateTCTable_h'>
			<tr>
				<th colspan='12' id="th_tcID">&nbsp;</th>
			</tr>
			<tr>
				<th>#</th>
				<th>系統識別代號</th>
				<th>作用<br/>案例</th>
				<th>測試案例名稱</th>
				<th>驗証重點數</th>
				<th>驗証重點</th>
				<th>測試資料</th>
				<th>測試流程</th>
				<th>預期結果</th>
				<th>建立日期</th>
				<th>建立者</th>
				<th>&nbsp;</th>
			</tr>
		</thead>

		<tbody id='updateTCTable_b'>

		</tbody>
	</table>
	
	<form id='q_form_mapping' method="post">
		<input type="hidden" id='q_tcID' name='q_tcID' value='' />
		<input type="hidden" id='presentUID' name='presentUID' value='' />
		<input type="hidden" id="loginUser" name="loginUser" />					<!-- 目前登入的使用者 -->
		<input type="hidden" id='clickUID' name='clickUID' value='' />
	</form>
	
	<!-- 記錄現在Active的UID -->
	<div id='square'></div>
	<div id="dialog-message" title="確認">
	</div>

</body>
</html>